<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
/* 
	分页
*/
class CI_Pagination{
	
	protected $CI;
	
	private $params = array();
	
	// 基础url
	public $url_base;
	
	// url追加
	public $url_suffix;
	
	// 页码
	public $page_num = 1;
	
	// 单页条数
	public $page_size;
	
	// 分页参数标识
	public $page_tag;
	
	// 总条数
	public $total_num;
	
	// 总页数
	public $total_page;
	
	// 起始数
	public $limit_start = 0;
	
	public $prev_str = '上一页';
	public $next_str = '下一页';
	
	public $show_jump = true;
	
	public function __construct($option = array()){
		$this->CI = &get_instance();
		$this->init($option);
	}
	
	public function init($option){
		$this->url_base = isset($option['url_base']) ? $option['url_base'] : '';
		$this->url_suffix = isset($option['url_suffix']) ? $option['url_suffix'] : '';
		$this->page_tag = isset($option['page_tag']) ? $option['page_tag'] : 'page_num';
		$this->page_size = isset($option['page_size']) ? $option['page_size'] : 30;
		$this->total_num = isset($option['total_num']) ? $option['total_num'] : 0;
		$this->set_total_page();
		$this->set_page_num();
	}
	
	public function create(){
		$output = '<span>共'.$this->total_num.'条 第'.$this->page_num.'/'.$this->total_page.'页</span>';
		$url_base = $this->url_base.'?'.$this->page_tag.'=';
		
		// 尾部追加
		$url_suffix = '';
		if($this->params){
			$url_suffix .= '&'.http_build_query($this->params);
		}
		$url_suffix .= $this->url_suffix;
		
		// 上一页
		if($this->page_num <= 1){
			$output .= '<span class="disabled">'.$this->prev_str.'</span>';
		}else{
			$output .= '<a href="'.$url_base.($this->page_num - 1).$url_suffix.'">'.$this->prev_str.'</a>';
		}
		
		// 页码
		$clip_num = 2; #左右页码数量
		$dot_num = $clip_num * 2 + 2;
		if($this->total_page <= $dot_num || $this->page_num < $dot_num){
			for($i = 1; $i <= $this->total_page && $i <= $dot_num; $i++) {
				$output .= ($i == $this->page_num) ? ('<span class="active">'.$i.'</span>') : '<a href="'.$url_base.$i.$url_suffix.'">'.$i.'</a>';
			}
			if($this->total_page > $dot_num && $this->page_num < $this->total_page){
				if(($this->total_page - $dot_num) > 1){
					$output .= '<span>...</span>';
				}
				$output .= '<a href="'.$url_base.$this->total_page.$url_suffix.'">'.$this->total_page.'</a>';
			}
		}
		else{
			$output .= '<a href="'.$url_base.'1'.$url_suffix.'">1</a><span>...</span>';
			for($i = $clip_num; $i > 0; $i--){
				$n = $this->page_num - $i;
				$output .= '<a href="'.$url_base.$n.$url_suffix.'">'.$n.'</a>';
			}
			$output .= '<span class="active">'.$this->page_num.'</span>';
			for($i = 1; $i <= $clip_num; $i++){
				$n = $this->page_num + $i;
				if($n > $this->total_page){
					break;
				}
				$output .= '<a href="'.$url_base.$n.$url_suffix.'">'.$n.'</a>';
			}
			if($n < $this->total_page){
				if(($this->total_page - $this->page_num) > ($clip_num + 1)){
					$output .= '<span>...</span>';
				}
				$output .= '<a href="'.$url_base.$this->total_page.$url_suffix.'">'.$this->total_page.'</a>';
			}
		}
		
		// 下一页
		if($this->page_num >= $this->total_page){
			$output .= '<span class="disabled">'.$this->next_str.'</span>';
		}else{
			$output .= '<a href="'.$url_base.($this->page_num + 1).$url_suffix.'">'.$this->next_str.'</a>';
		}
		
		if($this->show_jump){
			$output .= '<span class="jump">到第<input class="num" type="text" value="'.$this->page_num.'">页<a class="btn" href="javascript:;">确定</a></span>';
		}
		
		return $output;
	}
	
	public function set_page_num($num = null){
		$params = $this->CI->input->get();
		$this->page_num = $num ? $num : (int)$params[$this->page_tag];
		unset($params[$this->page_tag]);
		if($this->page_num <= 1){
			$this->page_num = 1;
		}else if($this->page_num >= $this->total_page){
			$this->page_num = $this->total_page;
		}
		$this->params = $params;
		$this->set_limit_start();
	}
	
	private function set_total_page(){
		if($this->total_num < $this->page_size){
			$this->total_page = 1;
		}else{
			$this->total_page = (int)ceil($this->total_num / $this->page_size);
		}
	}
	
	private function set_limit_start(){
		$this->limit_start = ($this->page_num - 1) * $this->page_size;
	}
	
}